<template>
    <div style="padding: 6px">
        <el-card v-show="showSearch" style="margin-bottom: 6px">
            <el-form v-show="showSearch" ref="queryForm" :inline="true" :model="queryParams" label-width="68px" style="margin-bottom: -20px">
                <el-form-item :label="$t('alert.index.236501-0')" prop="alertName">
                    <el-input v-model="queryParams.alertName" :placeholder="$t('alert.log.491272-1')" clearable size="small" @keyup.enter.native="handleQuery" />
                </el-form-item>
                <el-form-item :label="$t('alert.index.236501-2')" prop="alertLevel">
                    <el-select v-model="queryParams.alertLevel" :placeholder="$t('alert.index.236501-3')" clearable size="small">
                        <el-option v-for="dict in dict.type.iot_alert_level" :key="dict.value" :label="dict.label" :value="dict.value" />
                    </el-select>
                </el-form-item>
                <el-form-item :label="$t('alert.log.491272-4')" prop="status">
                    <el-select v-model="queryParams.status" :placeholder="$t('alert.log.491272-5')" clearable size="small">
                        <el-option v-for="dict in dict.type.iot_process_status" :key="dict.value" :label="dict.label" :value="dict.value" />
                    </el-select>
                </el-form-item>
                <el-form-item>
                    <el-button icon="el-icon-search" size="mini" type="primary" @click="handleQuery">{{ $t('search') }}</el-button>
                    <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">{{ $t('reset') }}</el-button>
                </el-form-item>
            </el-form>
        </el-card>

        <el-card class="main-card">
            <el-table :border="false" v-loading="loading" :data="alertLogList" @selection-change="handleSelectionChange">
                <el-table-column :label="$t('alert.index.236501-0')" align="center" prop="alertName" />
                <el-table-column :label="$t('alert.log.491272-8')" align="center" prop="serialNumber" />
                <el-table-column :label="$t('alert.log.491272-9')" align="center" prop="deviceName" />
                <el-table-column :label="$t('alert.index.236501-2')" align="center" prop="alertLevel" width="120">
                    <template slot-scope="scope">
                        <dict-tag :options="dict.type.iot_alert_level" :value="scope.row.alertLevel" />
                    </template>
                </el-table-column>
                <el-table-column :label="$t('alert.log.491272-10')" align="center" prop="createTime" width="170">
                    <template slot-scope="scope">
                        <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
                    </template>
                </el-table-column>
                <el-table-column :label="$t('alert.log.491272-24')" align="left" header-align="center" prop="detail">
                    <template slot-scope="scope">
                        <div v-html="formatDetail(scope.row.detail)"></div>
                    </template>
                </el-table-column>
                <el-table-column :label="$t('alert.log.491272-4')" align="center" prop="status">
                    <template slot-scope="scope">
                        <dict-tag :options="dict.type.iot_process_status" :value="scope.row.status" />
                    </template>
                </el-table-column>
                <el-table-column :label="$t('opation')" align="center" width="100">
                    <template slot-scope="scope">
                        <el-button v-hasPermi="['iot:alertLog:edit']" icon="el-icon-edit" size="mini" type="text" @click="handleUpdate(scope.row)">{{ $t('alert.log.491272-25') }}</el-button>
                    </template>
                </el-table-column>
            </el-table>

            <pagination v-show="total > 0" :limit.sync="queryParams.pageSize" :page.sync="queryParams.pageNum" :total="total" @pagination="getList" />

            <!-- 添加或修改设备告警对话框 -->
            <el-dialog :title="title" :visible.sync="open" append-to-body width="500px">
                <el-form ref="form" :model="form" :rules="rules" label-width="80px">
                    <el-form-item :label="$t('alert.log.491272-26')" prop="remark">
                        <el-input v-model="form.remark" :placeholder="$t('notify.log.333543-18')" rows="8" type="textarea" />
                    </el-form-item>
                </el-form>
                <div slot="footer" class="dialog-footer">
                    <el-button type="primary" @click="submitForm">{{ $t('confirm') }}</el-button>
                    <el-button @click="cancel">{{ $t('cancel') }}</el-button>
                </div>
            </el-dialog>
        </el-card>
    </div>
</template>

<script>
import { listAlertLog, getAlertLog, delAlertLog, addAlertLog, updateAlertLog } from '@/api/iot/alertLog';

export default {
    name: 'SceneLog',
    dicts: ['iot_alert_level', 'iot_process_status'],
    data() {
        return {
            // 遮罩层
            loading: true,
            // 选中数组
            ids: [],
            // 非单个禁用
            single: true,
            // 非多个禁用
            multiple: true,
            // 显示搜索条件
            showSearch: true,
            // 总条数
            total: 0,
            // 设备告警表格数据
            alertLogList: [],
            // 弹出层标题
            title: '',
            // 是否显示弹出层
            open: false,
            // 查询参数
            queryParams: {
                pageNum: 1,
                pageSize: 10,
                alertName: null,
                alertLevel: null,
                status: null,
                productId: null,
                productName: null,
                deviceId: null,
                deviceName: null,
            },
            // 表单参数
            form: {},
            // 表单校验
            rules: {
                remark: [
                    {
                        required: true,
                        message: this.$t('alert.log.491272-18'),
                        trigger: 'blur',
                    },
                ],
            },
        };
    },
    created() {
        this.getList();
    },
    methods: {
        /** 查询设备告警列表 */
        getList() {
            this.loading = true;
            listAlertLog(this.queryParams).then((response) => {
                this.alertLogList = response.rows;
                this.total = response.total;
                this.loading = false;
            });
        },
        // 取消按钮
        cancel() {
            this.open = false;
            this.reset();
        },
        // 表单重置
        reset() {
            this.form = {
                alertLogId: null,
                alertName: null,
                alertLevel: null,
                status: null,
                productId: null,
                productName: null,
                deviceId: null,
                deviceName: null,
                createBy: null,
                createTime: null,
                updateBy: null,
                updateTime: null,
                remark: null,
            };
            this.resetForm('form');
        },
        /** 搜索按钮操作 */
        handleQuery() {
            this.queryParams.pageNum = 1;
            this.getList();
        },
        /** 重置按钮操作 */
        resetQuery() {
            this.resetForm('queryForm');
            this.handleQuery();
        },
        // 多选框选中数据
        handleSelectionChange(selection) {
            this.ids = selection.map((item) => item.alertLogId);
            this.single = selection.length !== 1;
            this.multiple = !selection.length;
        },
        /** 新增按钮操作 */
        handleAdd() {
            this.reset();
            this.open = true;
            this.title = this.$t('alert.log.491272-19');
        },
        /** 修改按钮操作 */
        handleUpdate(row) {
            this.reset();
            const alertLogId = row.alertLogId || this.ids;
            getAlertLog(alertLogId).then((response) => {
                this.form = response.data;
                this.open = true;
                this.title = this.$t('alert.log.491272-20');
            });
        },
        /** 提交按钮 */
        submitForm() {
            this.$refs['form'].validate((valid) => {
                if (valid) {
                    if (this.form.alertLogId != null) {
                        updateAlertLog(this.form).then((response) => {
                            this.$modal.msgSuccess(this.$t('updateSuccess'));
                            this.open = false;
                            this.getList();
                        });
                    } else {
                        addAlertLog(this.form).then((response) => {
                            this.$modal.msgSuccess(this.$t('addSuccess'));
                            this.open = false;
                            this.getList();
                        });
                    }
                }
            });
        },
        /** 删除按钮操作 */
        handleDelete(row) {
            const alertLogIds = row.alertLogId || this.ids;
            this.$modal
                .confirm(this.$t('alert.log.491272-23', [alertLogIds]))
                .then(function () {
                    return delAlertLog(alertLogIds);
                })
                .then(() => {
                    this.getList();
                    this.$modal.msgSuccess(this.$t('delSuccess'));
                })
                .catch(() => {});
        },
        /** 导出按钮操作 */
        handleExport() {
            this.download(
                'iot/alertLog/export',
                {
                    ...this.queryParams,
                },
                `alertLog_${new Date().getTime()}.xlsx`
            );
        },
        /**格式化显示物模型**/
        formatDetail(json) {
            if (json == null || json == '') {
                return;
            }
            let item = JSON.parse(json);
            let result = 'id：<span style="color:#F56C6C">' + item.id + '</span><br />';
            result = result + 'value：<span style="color:#F56C6C">' + item.value + '</span><br />';
            result = result + 'remark：<span style="color:#F56C6C">' + item.remark + '</span>';
            return result;
        },
    },
};
</script>
